import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/login' // 重定向会改变地址栏里路由地址, 然后导致路由重新匹配
  },
  {
    path: '/login',
    component: () => import('@/views/Login')
    // 路由懒加载: webpack提供import函数, 可以在路由路径匹配后, 再去引入对应的组件
  },
  {
    path: '/layout',
    component: () => import('@/views/Layout'),
    redirect: '/layout/home', // 立刻做重定向
    children: [
      {
        path: 'home',
        component: () => import('@/views/Home')
      },
      {
        path: 'user',
        component: () => import('@/views/User')
      }
    ]
  },
  {
    path: '/search',
    component: () => import('@/views/Search')
  },
  {
    path: '/search/:keywords', // 路径传参需要先在规则里用冒号定义变量名(确定第几段路径是值)->跳转时必须是2段才能命中这个规则
    component: () => import('@/views/Search/SearchResult.vue')
  },
  {
    path: '/article_detail',
    component: () => import('@/views/ArticleDetail')
  },
  {
    path: '/user_editor',
    component: () => import('@/views/User/UserEdit.vue')
  }
]

const router = new VueRouter({
  routes
})

export default router
